package org.jeecg.modules.front.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.*;
import org.jeecg.modules.front.entity.vo.*;
import org.jeecg.modules.fund.entity.FundType;
import org.jeecg.modules.fund.vo.FundPage;
import org.jeecg.modules.gongqiang.entity.Userhome;
import org.jeecg.modules.gongqiang.entity.UserhomeAuthentication;
import org.jeecg.modules.gongqiang.service.IUserhomeAuthenticationService;
import org.jeecg.modules.gongqiang.service.IUserhomeService;
import org.jeecg.modules.gongqiang.service.IYcYingshengUserwalltService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Random;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/front/authentication")
@Api(tags = "前台用户实名认证模块")
@Slf4j
public class AuthenticationController {
    @Autowired
    private IUserhomeService userhomeService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IUserhomeAuthenticationService userhomeAuthenticationService;

    @ApiOperation("实名认证接口")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<String> add(@RequestBody AuthenticationUserhome authenticationUserhome,HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        Userhome userhome = userhomeService.getById(userId);
        String code = authenticationUserhome.getCode();
        if(oConvertUtils.isEmpty(userhome))
        {
            return Result.error("用户不存在");
        }
        if(userhome.getIsAuthentication()==1)
        {
            return Result.error("用户已经实名了");
        }
        if (code == null) {
            return Result.error("邮箱验证码无效");
        }
        Object checkCode = redisUtil.get("FULSMS" + authenticationUserhome.getPhone());
        if (checkCode == null || !checkCode.toString().equals(code)) {
            return Result.error("邮箱验证码无效");
        }
        redisUtil.del("FULSMS" + authenticationUserhome.getPhone());
        return userhomeAuthenticationService.authentication(authenticationUserhome,userhome.getId());
    }

    @ApiOperation("查询实名认证信息")
    @RequestMapping(value = "/authenticationInfo", method = RequestMethod.GET)
    public Result<UserhomeAuthentication> authenticationInfo(HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        QueryWrapper<UserhomeAuthentication> userhomeAuthenticationQueryWrapper =new QueryWrapper<>();
        userhomeAuthenticationQueryWrapper.eq("userhome_id",userId);
        UserhomeAuthentication userhomeAuthentication =userhomeAuthenticationService.getOne(userhomeAuthenticationQueryWrapper);
        if(oConvertUtils.isEmpty(userhomeAuthentication))
        {
            return Result.error("用户实名信息不正确");
        }

        return Result.OK(userhomeAuthentication);
    }
}